package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.Date;
import java.sql.SQLException;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Meetstation;
import model.Meting;
import model.Model;
import dao.MetingDAO;

public class MeetgegevensView extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JButton updateButton = null;

	private Model hetModel = null;

	private JButton newButton = null;
	private JButton deleteButton = null;
	private JPanel jPanel2 = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JLabel jLabel5 = null;
	private JLabel jLabel6 = null;
	private JTextField stationField = null;
	private JTextField datumField = null;
	private JTextField windrichtingField = null;
	private JTextField gemWindField = null;
	private JTextField maxWindField = null;
	private JTextField minWindField = null;
	private JLabel jLabel7 = null;
	private JLabel jLabel8 = null;
	private JLabel jLabel9 = null;
	private JLabel jLabel10 = null;
	private JLabel jLabel11 = null;
	private JLabel jLabel12 = null;
	private JLabel jLabel13 = null;
	private JLabel jLabel14 = null;
	private JLabel jLabel15 = null;
	private JTextField duurNeerslagField = null;
	private JTextField somNeerslagField = null;
	private JTextField gemTempField = null;
	private JTextField maxTempField = null;
	private JTextField minTempField = null;
	private JTextField minTemp10Field = null;
	private JTextField gemLuchtField = null;
	private JTextField maxLuchtField = null;
	private JTextField minLuchtField = null;
	private JPanel jPanel3 = null;
	private JPanel jPanel4 = null;
	private JLabel jLabel = null;
	private JComboBox dagBox = null;
	private JComboBox maandBox = null;
	private JComboBox jaarBox = null;
	private boolean initialized = false;

	/**
	 * This is the default constructor
	 */
	public MeetgegevensView(Model model) {
		super();
		this.hetModel = model;

		// Luisteraar voor de wijziging van het station
		hetModel.luisteraarToevoegen("station", new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				Meetstation m = (Meetstation) evt.getNewValue();
				if (m != null) {
					// Maak een java.sql.Date object
					int dag = (Integer) dagBox.getSelectedItem();
					int maand = maandBox.getSelectedIndex();
					int jaar = (Integer) jaarBox.getSelectedItem();

					final Calendar cal = Calendar.getInstance();
					cal.set(jaar, maand, dag);
					java.sql.Date datum = new Date(cal.getTimeInMillis());

					System.out.println("" + datum);

					setMeting(datum);
				}
			}
		});

		hetModel.luisteraarToevoegen("meting", new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				Meting meting = (Meting) evt.getNewValue();
				if (meting != null) {
					laadMeting(meting);
				}
			}
		});

		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(442, 309);
		this.setLayout(new BorderLayout());
		this.add(getJPanel(), BorderLayout.NORTH);
		this.add(getJPanel1(), BorderLayout.SOUTH);
		this.add(getJPanel2(), BorderLayout.CENTER);

		// Initialize datum selector
		for (int i = 1; i < 32; i++) {
			dagBox.addItem(i);
		}

		for (int j = 1; j < 13; j++) {
			maandBox.addItem(j);
		}

		for (int k = 1950; k < 2010; k++) {
			jaarBox.addItem(k);
		}
		dagBox.setSelectedIndex(1);
		maandBox.setSelectedIndex(4);
		jaarBox.setSelectedIndex(jaarBox.getItemCount() - 1);
		initialized = true;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.gridy = 4;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 2;
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(getJPanel3(), BorderLayout.NORTH);
			jPanel.add(getJPanel4(), BorderLayout.SOUTH);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints2.gridy = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 2;
			gridBagConstraints1.gridy = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints.gridy = 1;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(getUpdateButton(), gridBagConstraints1);
			jPanel1.add(getNewButton(), gridBagConstraints);
			jPanel1.add(getDeleteButton(), gridBagConstraints2);
		}
		return jPanel1;
	}

	/**
	 * This method initializes updateButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getUpdateButton() {
		if (updateButton == null) {
			updateButton = new JButton();
			updateButton.setText("UPDATE");
			updateButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					int stationid = Integer.parseInt(stationField.getText()
							.toString().trim());

					String strDate = datumField.getText();
					java.sql.Date datum = null;
					int year = Integer.parseInt(strDate.substring(0, 4));
					int month = Integer.parseInt(strDate.substring(5, 7));
					System.out.println(month);
					int day = Integer.parseInt(strDate.substring(8, 10));

					System.out.println("" + year + "-" + month + "-" + day);

					final Calendar cal = Calendar.getInstance();
					cal.set(year, month - 1, day);
					datum = new Date(cal.getTimeInMillis());

					int windrichting = Integer.parseInt(windrichtingField
							.getText().toString().trim());
					int gemwind = Integer.parseInt(gemWindField.getText()
							.toString().trim());
					int maxwind = Integer.parseInt(maxWindField.getText()
							.toString().trim());
					int minwind = Integer.parseInt(minWindField.getText()
							.toString().trim());
					int duurneerslag = Integer.parseInt(duurNeerslagField
							.getText().toString().trim());
					int somneerslag = Integer.parseInt(somNeerslagField
							.getText().toString().trim());
					int gemtemp = Integer.parseInt(gemTempField.getText()
							.toString().trim());
					int maxtemp = Integer.parseInt(maxTempField.getText()
							.toString().trim());
					int mintemp = Integer.parseInt(minTempField.getText()
							.toString().trim());
					int mintemp10 = Integer.parseInt(minTemp10Field.getText()
							.toString().trim());
					int gemdruk = Integer.parseInt(gemLuchtField.getText()
							.toString().trim());
					int maxdruk = Integer.parseInt(maxLuchtField.getText()
							.toString().trim());
					int mindruk = Integer.parseInt(minLuchtField.getText()
							.toString().trim());

					Meting oudeMeting = hetModel.getMeting();

					Meting nieuweMeting = new Meting(stationid, datum,
							windrichting, gemwind, maxwind, minwind, gemtemp,
							maxtemp, mintemp, mintemp10, gemdruk, maxdruk,
							mindruk, duurneerslag, somneerslag);

					try {
						(new MetingDAO()).updateMeting(oudeMeting
								.getStation_id(), (new java.sql.Date(oudeMeting
								.getDatum().getTime())), nieuweMeting);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
		}
		return updateButton;
	}

	/**
	 * This method initializes nwRowButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getNewButton() {
		if (newButton == null) {
			newButton = new JButton();
			newButton.setText("NEW");
			newButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					// TODO: Controlleer of de geselecteerde datum nog niet
					// bestaat in de database
					// TODO: Controlleer of alles is ingevuld

					int stationid = Integer.parseInt(stationField.getText()
							.toString().trim());

					String strDate = datumField.getText();
					java.sql.Date datum = null;
					int year = Integer.parseInt(strDate.substring(0, 4));
					int month = Integer.parseInt(strDate.substring(5, 7));
					System.out.println(month);
					int day = Integer.parseInt(strDate.substring(8, 10));

					System.out.println("" + year + "-" + month + "-" + day);

					final Calendar cal = Calendar.getInstance();
					cal.set(year, month - 1, day);
					datum = new Date(cal.getTimeInMillis());

					int windrichting = Integer.parseInt(windrichtingField
							.getText().toString().trim());
					int gemwind = Integer.parseInt(gemWindField.getText()
							.toString().trim());
					int maxwind = Integer.parseInt(maxWindField.getText()
							.toString().trim());
					int minwind = Integer.parseInt(minWindField.getText()
							.toString().trim());
					int duurneerslag = Integer.parseInt(duurNeerslagField
							.getText().toString().trim());
					int somneerslag = Integer.parseInt(somNeerslagField
							.getText().toString().trim());
					int gemtemp = Integer.parseInt(gemTempField.getText()
							.toString().trim());
					int maxtemp = Integer.parseInt(maxTempField.getText()
							.toString().trim());
					int mintemp = Integer.parseInt(minTempField.getText()
							.toString().trim());
					int mintemp10 = Integer.parseInt(minTemp10Field.getText()
							.toString().trim());
					int gemdruk = Integer.parseInt(gemLuchtField.getText()
							.toString().trim());
					int maxdruk = Integer.parseInt(maxLuchtField.getText()
							.toString().trim());
					int mindruk = Integer.parseInt(minLuchtField.getText()
							.toString().trim());

					Meting nieuweMeting = new Meting(stationid, datum,
							windrichting, gemwind, maxwind, minwind, gemtemp,
							maxtemp, mintemp, mintemp10, gemdruk, maxdruk,
							mindruk, duurneerslag, somneerslag);

					try {
						(new MetingDAO()).newMeting(nieuweMeting);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
		}
		return newButton;
	}

	/**
	 * This method initializes deleteRowButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setText("DELETE");
			deleteButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					Meting m = hetModel.getMeting();

					try {
						(new MetingDAO()).deleteMeting(m.getStation_id(),
								(new java.sql.Date(m.getDatum().getTime())));
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					clearMetingen();
				}
			});
		}
		return deleteButton;
	}

	/**
	 * This method initializes jPanel2
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints36 = new GridBagConstraints();
			gridBagConstraints36.fill = GridBagConstraints.BOTH;
			gridBagConstraints36.gridy = 7;
			gridBagConstraints36.weightx = 1.0;
			gridBagConstraints36.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints36.gridx = 3;
			GridBagConstraints gridBagConstraints35 = new GridBagConstraints();
			gridBagConstraints35.fill = GridBagConstraints.BOTH;
			gridBagConstraints35.gridy = 6;
			gridBagConstraints35.weightx = 1.0;
			gridBagConstraints35.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints35.gridx = 3;
			GridBagConstraints gridBagConstraints34 = new GridBagConstraints();
			gridBagConstraints34.fill = GridBagConstraints.BOTH;
			gridBagConstraints34.gridy = 5;
			gridBagConstraints34.weightx = 1.0;
			gridBagConstraints34.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints34.gridx = 3;
			GridBagConstraints gridBagConstraints33 = new GridBagConstraints();
			gridBagConstraints33.fill = GridBagConstraints.BOTH;
			gridBagConstraints33.gridy = 4;
			gridBagConstraints33.weightx = 1.0;
			gridBagConstraints33.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints33.gridx = 3;
			GridBagConstraints gridBagConstraints32 = new GridBagConstraints();
			gridBagConstraints32.fill = GridBagConstraints.BOTH;
			gridBagConstraints32.gridy = 3;
			gridBagConstraints32.weightx = 1.0;
			gridBagConstraints32.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints32.gridx = 3;
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.fill = GridBagConstraints.BOTH;
			gridBagConstraints31.gridy = 2;
			gridBagConstraints31.weightx = 1.0;
			gridBagConstraints31.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints31.gridx = 3;
			GridBagConstraints gridBagConstraints30 = new GridBagConstraints();
			gridBagConstraints30.fill = GridBagConstraints.BOTH;
			gridBagConstraints30.gridy = 1;
			gridBagConstraints30.weightx = 1.0;
			gridBagConstraints30.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints30.gridx = 3;
			GridBagConstraints gridBagConstraints29 = new GridBagConstraints();
			gridBagConstraints29.fill = GridBagConstraints.BOTH;
			gridBagConstraints29.gridy = 8;
			gridBagConstraints29.weightx = 1.0;
			gridBagConstraints29.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints29.gridx = 1;
			GridBagConstraints gridBagConstraints28 = new GridBagConstraints();
			gridBagConstraints28.fill = GridBagConstraints.BOTH;
			gridBagConstraints28.gridy = 7;
			gridBagConstraints28.weightx = 1.0;
			gridBagConstraints28.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints28.gridx = 1;
			GridBagConstraints gridBagConstraints27 = new GridBagConstraints();
			gridBagConstraints27.gridx = 2;
			gridBagConstraints27.anchor = GridBagConstraints.WEST;
			gridBagConstraints27.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints27.gridy = 7;
			jLabel15 = new JLabel();
			jLabel15.setText("minLuchtdruk");
			GridBagConstraints gridBagConstraints26 = new GridBagConstraints();
			gridBagConstraints26.gridx = 0;
			gridBagConstraints26.anchor = GridBagConstraints.WEST;
			gridBagConstraints26.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints26.gridy = 8;
			jLabel14 = new JLabel();
			jLabel14.setText("somNeerslag");
			GridBagConstraints gridBagConstraints25 = new GridBagConstraints();
			gridBagConstraints25.gridx = 0;
			gridBagConstraints25.anchor = GridBagConstraints.WEST;
			gridBagConstraints25.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints25.gridy = 7;
			jLabel13 = new JLabel();
			jLabel13.setText("duurNeerslag");
			GridBagConstraints gridBagConstraints24 = new GridBagConstraints();
			gridBagConstraints24.gridx = 2;
			gridBagConstraints24.anchor = GridBagConstraints.WEST;
			gridBagConstraints24.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints24.gridy = 6;
			jLabel12 = new JLabel();
			jLabel12.setText("maxLuchtdruk");
			GridBagConstraints gridBagConstraints23 = new GridBagConstraints();
			gridBagConstraints23.gridx = 2;
			gridBagConstraints23.anchor = GridBagConstraints.WEST;
			gridBagConstraints23.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints23.gridy = 5;
			jLabel11 = new JLabel();
			jLabel11.setText("gemLuchtdruk");
			GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
			gridBagConstraints22.gridx = 2;
			gridBagConstraints22.anchor = GridBagConstraints.WEST;
			gridBagConstraints22.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints22.gridy = 4;
			jLabel10 = new JLabel();
			jLabel10.setText("minTemperatuur10");
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 2;
			gridBagConstraints21.anchor = GridBagConstraints.WEST;
			gridBagConstraints21.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints21.gridy = 3;
			jLabel9 = new JLabel();
			jLabel9.setText("minTemperatuur");
			GridBagConstraints gridBagConstraints20 = new GridBagConstraints();
			gridBagConstraints20.gridx = 2;
			gridBagConstraints20.anchor = GridBagConstraints.WEST;
			gridBagConstraints20.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints20.gridy = 2;
			jLabel8 = new JLabel();
			jLabel8.setText("maxTemperatuur");
			GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
			gridBagConstraints19.gridx = 2;
			gridBagConstraints19.anchor = GridBagConstraints.WEST;
			gridBagConstraints19.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints19.gridy = 1;
			jLabel7 = new JLabel();
			jLabel7.setText("gemTemperatuur");
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.fill = GridBagConstraints.BOTH;
			gridBagConstraints18.gridy = 6;
			gridBagConstraints18.weightx = 1.0;
			gridBagConstraints18.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints18.gridx = 1;
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.fill = GridBagConstraints.BOTH;
			gridBagConstraints17.gridy = 5;
			gridBagConstraints17.weightx = 1.0;
			gridBagConstraints17.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints17.gridx = 1;
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.fill = GridBagConstraints.BOTH;
			gridBagConstraints16.gridy = 4;
			gridBagConstraints16.weightx = 1.0;
			gridBagConstraints16.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints16.gridx = 1;
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.fill = GridBagConstraints.BOTH;
			gridBagConstraints15.gridy = 3;
			gridBagConstraints15.weightx = 1.0;
			gridBagConstraints15.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints15.gridx = 1;
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.fill = GridBagConstraints.BOTH;
			gridBagConstraints14.gridy = 2;
			gridBagConstraints14.weightx = 1.0;
			gridBagConstraints14.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints14.gridx = 1;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.fill = GridBagConstraints.BOTH;
			gridBagConstraints13.gridy = 1;
			gridBagConstraints13.weightx = 1.0;
			gridBagConstraints13.insets = new Insets(1, 0, 1, 10);
			gridBagConstraints13.gridx = 1;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.anchor = GridBagConstraints.WEST;
			gridBagConstraints12.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints12.gridy = 6;
			jLabel6 = new JLabel();
			jLabel6.setText("minWindsnelheid");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.anchor = GridBagConstraints.WEST;
			gridBagConstraints11.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints11.gridy = 5;
			jLabel5 = new JLabel();
			jLabel5.setText("maxWindsnelheid");
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.anchor = GridBagConstraints.WEST;
			gridBagConstraints10.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints10.gridy = 4;
			jLabel4 = new JLabel();
			jLabel4.setText("gemWindsnelheid");
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints9.gridy = 3;
			jLabel3 = new JLabel();
			jLabel3.setText("windrichting");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints7.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("datum");
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints6.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("station_id");
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.add(jLabel1, gridBagConstraints6);
			jPanel2.add(jLabel2, gridBagConstraints7);
			jPanel2.add(jLabel3, gridBagConstraints9);
			jPanel2.add(jLabel4, gridBagConstraints10);
			jPanel2.add(jLabel5, gridBagConstraints11);
			jPanel2.add(jLabel6, gridBagConstraints12);
			jPanel2.add(getStationField(), gridBagConstraints13);
			jPanel2.add(getDatumField(), gridBagConstraints14);
			jPanel2.add(getWindrichtingField(), gridBagConstraints15);
			jPanel2.add(getGemWindField(), gridBagConstraints16);
			jPanel2.add(getMaxWindField(), gridBagConstraints17);
			jPanel2.add(getMinWindField(), gridBagConstraints18);
			jPanel2.add(jLabel7, gridBagConstraints19);
			jPanel2.add(jLabel8, gridBagConstraints20);
			jPanel2.add(jLabel9, gridBagConstraints21);
			jPanel2.add(jLabel10, gridBagConstraints22);
			jPanel2.add(jLabel11, gridBagConstraints23);
			jPanel2.add(jLabel12, gridBagConstraints24);
			jPanel2.add(jLabel13, gridBagConstraints25);
			jPanel2.add(jLabel14, gridBagConstraints26);
			jPanel2.add(jLabel15, gridBagConstraints27);
			jPanel2.add(getDuurNeerslagField(), gridBagConstraints28);
			jPanel2.add(getSomNeerslagField(), gridBagConstraints29);
			jPanel2.add(getGemTempField(), gridBagConstraints30);
			jPanel2.add(getMaxTempField(), gridBagConstraints31);
			jPanel2.add(getMinTempField(), gridBagConstraints32);
			jPanel2.add(getMinTemp10Field(), gridBagConstraints33);
			jPanel2.add(getGemLuchtField(), gridBagConstraints34);
			jPanel2.add(getMaxLuchtField(), gridBagConstraints35);
			jPanel2.add(getMinLuchtField(), gridBagConstraints36);
		}
		return jPanel2;
	}

	/**
	 * This method initializes stationField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getStationField() {
		if (stationField == null) {
			stationField = new JTextField();
		}
		return stationField;
	}

	/**
	 * This method initializes datumField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getDatumField() {
		if (datumField == null) {
			datumField = new JTextField();
		}
		return datumField;
	}

	/**
	 * This method initializes windrichtingField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getWindrichtingField() {
		if (windrichtingField == null) {
			windrichtingField = new JTextField();
		}
		return windrichtingField;
	}

	/**
	 * This method initializes gemWindField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getGemWindField() {
		if (gemWindField == null) {
			gemWindField = new JTextField();
		}
		return gemWindField;
	}

	/**
	 * This method initializes maxWindField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMaxWindField() {
		if (maxWindField == null) {
			maxWindField = new JTextField();
		}
		return maxWindField;
	}

	/**
	 * This method initializes minWindField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMinWindField() {
		if (minWindField == null) {
			minWindField = new JTextField();
		}
		return minWindField;
	}

	/**
	 * This method initializes duurNeerslagField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getDuurNeerslagField() {
		if (duurNeerslagField == null) {
			duurNeerslagField = new JTextField();
		}
		return duurNeerslagField;
	}

	/**
	 * This method initializes somNeerslagField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getSomNeerslagField() {
		if (somNeerslagField == null) {
			somNeerslagField = new JTextField();
		}
		return somNeerslagField;
	}

	/**
	 * This method initializes gemTempField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getGemTempField() {
		if (gemTempField == null) {
			gemTempField = new JTextField();
		}
		return gemTempField;
	}

	/**
	 * This method initializes maxTempField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMaxTempField() {
		if (maxTempField == null) {
			maxTempField = new JTextField();
		}
		return maxTempField;
	}

	/**
	 * This method initializes minTempField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMinTempField() {
		if (minTempField == null) {
			minTempField = new JTextField();
		}
		return minTempField;
	}

	/**
	 * This method initializes minTemp10Field
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMinTemp10Field() {
		if (minTemp10Field == null) {
			minTemp10Field = new JTextField();
		}
		return minTemp10Field;
	}

	/**
	 * This method initializes gemLuchtField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getGemLuchtField() {
		if (gemLuchtField == null) {
			gemLuchtField = new JTextField();
		}
		return gemLuchtField;
	}

	/**
	 * This method initializes maxLuchtField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMaxLuchtField() {
		if (maxLuchtField == null) {
			maxLuchtField = new JTextField();
		}
		return maxLuchtField;
	}

	/**
	 * This method initializes minLuchtField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMinLuchtField() {
		if (minLuchtField == null) {
			minLuchtField = new JTextField();
		}
		return minLuchtField;
	}

	/**
	 * This method initializes jPanel3
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			jLabel = new JLabel();
			jLabel.setText("Metingen");
			jLabel.setFont(new Font("Dialog", Font.BOLD, 18));
			jPanel3 = new JPanel();
			jPanel3.setLayout(new GridBagLayout());
			jPanel3.add(jLabel, new GridBagConstraints());
		}
		return jPanel3;
	}

	/**
	 * This method initializes jPanel4
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(5);
			jPanel4 = new JPanel();
			jPanel4.setLayout(flowLayout);
			jPanel4.add(getDagBox(), null);
			jPanel4.add(getMaandBox(), null);
			jPanel4.add(getJaarBox(), null);
		}
		return jPanel4;
	}

	/**
	 * This method initializes dagBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getDagBox() {
		if (dagBox == null) {
			dagBox = new JComboBox();
			dagBox.setMaximumRowCount(8);
			dagBox.setPreferredSize(new Dimension(50, 25));
			dagBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					if (initialized == true) {
						System.out.println("actionPerformed()");
						// TODO Auto-generated Event stub actionPerformed()

						// Maak een java.sql.Date object
						int dag = (Integer) dagBox.getSelectedItem();
						int maand = maandBox.getSelectedIndex();
						int jaar = (Integer) jaarBox.getSelectedItem();

						final Calendar cal = Calendar.getInstance();
						cal.set(jaar, maand, dag);
						java.sql.Date datum = new Date(cal.getTimeInMillis());

						System.out.println("" + datum);

						setMeting(datum);
					}

				}
			});
		}
		return dagBox;
	}

	/**
	 * This method initializes maandBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getMaandBox() {
		if (maandBox == null) {
			maandBox = new JComboBox();
			maandBox.setPreferredSize(new Dimension(125, 25));
			maandBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					if (initialized == true) {
						System.out.println("actionPerformed()");
						// TODO Auto-generated Event stub actionPerformed()

						// Laad nieuwe metingen
						// Maak een java.sql.Date object
						int dag = (Integer) dagBox.getSelectedItem();
						int maand = maandBox.getSelectedIndex();
						int jaar = (Integer) jaarBox.getSelectedItem();

						final Calendar cal = Calendar.getInstance();
						cal.set(jaar, maand, dag);
						java.sql.Date datum = new Date(cal.getTimeInMillis());

						System.out.println("" + datum);

						setMeting(datum);
					}
				}
			});
		}
		return maandBox;
	}

	/**
	 * This method initializes jaarBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJaarBox() {
		if (jaarBox == null) {
			jaarBox = new JComboBox();
			jaarBox.setPreferredSize(new Dimension(75, 25));
			jaarBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					if (initialized == true) {
						System.out.println("actionPerformed()");
						// TODO Auto-generated Event stub actionPerformed()

						// Laad nieuwe metingen

						// Maak een java.sql.Date object
						int dag = (Integer) dagBox.getSelectedItem();
						int maand = maandBox.getSelectedIndex();
						int jaar = (Integer) jaarBox.getSelectedItem();

						final Calendar cal = Calendar.getInstance();
						cal.set(jaar, maand, dag);
						java.sql.Date datum = new Date(cal.getTimeInMillis());

						System.out.println("" + datum);

						setMeting(datum);
					}

				}
			});
		}
		return jaarBox;
	}

	private void setMeting(java.sql.Date datum) {
		// laad niewe metingen

		// Vraag het geselecteerde station op
		int station_id = hetModel.getMeetstation().getStationID();

		// Vraag de meetgegevens op
		MetingDAO metingDAO = new MetingDAO();
		Meting meting = null;
		try {
			meting = metingDAO.getMeting(station_id, datum);
			if (meting == null) {
				clearMetingen();
			}
		} catch (SQLException e1) {
			clearMetingen();
		}
		hetModel.setMeting(meting);
	}

	private void laadMeting(Meting m) {
		stationField.setText("" + m.getStation_id());
		datumField.setText(m.getDatum().toString());
		windrichtingField.setText("" + m.getWindrichting());
		gemWindField.setText("" + m.getGemWindsnelheid());
		maxWindField.setText("" + m.getMaxWindsnelheid());
		minWindField.setText("" + m.getMinWindsnelheid());
		duurNeerslagField.setText("" + m.getDuurNeerslag());
		somNeerslagField.setText("" + m.getSomNeerslag());
		gemTempField.setText("" + m.getGemTemperatuur());
		maxTempField.setText("" + m.getMaxTemperatuur());
		minTempField.setText("" + m.getMinTemperatuur());
		minTemp10Field.setText("" + m.getMinTemperatuur10());
		gemLuchtField.setText("" + m.getGemLuchtdruk());
		maxLuchtField.setText("" + m.getMaxLuchtdruk());
		minLuchtField.setText("" + m.getMinLuchtdruk());
	}

	private void clearMetingen() {
		stationField.setText("" + hetModel.getMeetstation().getStationID());
		datumField.setText("");
		windrichtingField.setText("");
		gemWindField.setText("");
		maxWindField.setText("");
		minWindField.setText("");
		duurNeerslagField.setText("");
		somNeerslagField.setText("");
		gemTempField.setText("");
		maxTempField.setText("");
		minTempField.setText("");
		minTemp10Field.setText("");
		gemLuchtField.setText("");
		maxLuchtField.setText("");
		minLuchtField.setText("");
	}
} // @jve:decl-index=0:visual-constraint="10,10"
